import Vue from 'vue'
import Vuex from 'vuex'
import common from '../utils/common'

Vue.use(Vuex)

// types
const types = {
    SET_BUYCART: "SET_BUYCART",
    ADD_BUYCART: "ADD_BUYCART",
    UPDATE_BUYCART: "UPDATE_BUYCART",
}

//state
const state = {
    buyCart: JSON.stringify({ }),
}

//getters
const getters = {
    buyCart: state => JSON.parse(state.buyCart),
}

//mutations
const mutations = {
    [types.SET_BUYCART](state, buyCart) {
        if (buyCart.totalPrice != 0 && buyCart != undefined) {
            state.buyCart = JSON.stringify(buyCart);
        } else {
            state.buyCart = JSON.stringify({ commodityList: [], totalPrice: 0 });
        }
    },
    [types.ADD_BUYCART](state, commodity) {
        var buyCart = JSON.parse(state.buyCart);
        var index = common.isExist(buyCart, commodity);
        var isExist = index != -1;
        var unitPrice;
        if (isExist) {
            buyCart.commodityList[index].purchase =
                parseInt(buyCart.commodityList[index].purchase) + parseInt(commodity.purchase);
            //获取单价
            unitPrice = common.getPrice(buyCart.commodityList[index]);
            buyCart.totalPrice =
                parseFloat(buyCart.totalPrice) +
                parseFloat(unitPrice) * parseInt(commodity.purchase);

            buyCart.commodityList[index].price =
                parseInt(buyCart.commodityList[index].purchase) * parseInt(unitPrice)
        } else {
            //获取单价
            unitPrice = common.getPrice(commodity);
            buyCart.totalPrice =
                parseFloat(buyCart.totalPrice) +
                parseFloat(unitPrice) * parseInt(commodity.purchase);
            buyCart.commodityList.push(commodity);
        }
        state.buyCart = JSON.stringify(buyCart);
    },
    [types.UPDATE_BUYCART](state, commodity) {
        var buyCart = JSON.parse(state.buyCart);
        var index = common.isExist(buyCart, commodity);
        //获取新数据单价
        var newPrice = common.getPrice(commodity);
        //获取旧数据单价
        var oldPrice = common.getPrice(buyCart.commodityList[index]);
        //更新价格
        console.log(parseFloat(oldPrice) * parseInt(buyCart.commodityList[index].purchase), " 旧商品 ")
        console.log(parseFloat(newPrice) * parseInt(commodity.purchase), " 新商品 ")
        console.log(buyCart.totalPrice, " 旧总价 ")
        buyCart.totalPrice =
            parseFloat(buyCart.totalPrice) -
            parseFloat(oldPrice) * parseInt(buyCart.commodityList[index].purchase) +
            parseFloat(newPrice) * parseInt(commodity.purchase);
        console.log(buyCart.totalPrice, " 新总价 ")
        commodity.price = parseInt(commodity.purchase) * parseFloat(newPrice)
        buyCart.commodityList[index] = commodity;
        state.buyCart = JSON.stringify(buyCart);
    }
}

//actions
const actions = {
    setCart: ({ commit }, cart) => {
        commit(types.SET_CART, cart);
    },
    addCart: ({ commit }, commodity) => {
        commit(types.ADD_CART, commodity);
    },
    updateCart: ({ commit }, commodity) => {
        commit(types.UPDATE_BUYCART, commodity);
    }
}

export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions
})